package demo3;



/**
 * @Author GocChin
 * @Date 2021/5/11 16:18
 * @Blog: itdfq.com
 * @QQ: 909256107
 * @Descript: 使用两个线程打印1-100 线程1 线程2 交替打印
 */

class  Number implements Runnable{

    private int number=1;

    @Override
    public void run() {
        while(true){
            synchronized (this){
                notify();
                if (number<=100){

                    System.out.println(Thread.currentThread().getName()+":"+number);
                    number++;
                    //使得调用wait()方法的线程进入阻塞状态。
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    break;
                }
            }


        }
    }
}
public class CommunicationTest {
    public static void main(String[] args) {
        Number number = new Number();
        Thread t1 = new Thread(number);
        Thread t2 = new Thread(number);
        t1.setName("线程1");
        t2.setName("线程2");
        t1.start();
        t2.start();

    }
}
